package com.nkzy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.DeptFromDTO;
import com.nkzy.domain.pojo.Dept;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.vo.DeptVO;
import com.nkzy.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@CrossOrigin
public class DeptController {
    @Autowired
    private DeptService deptService;
    //增加部门信息
    @PostMapping
    @Operation(summary = "增加部门信息")
    public Result saveDept(@RequestBody DeptFromDTO deptFromDTO) {
        Dept dept = BeanUtil.copyProperties(deptFromDTO, Dept.class);
        return Result.success(dept);
    }
    //删除员工信息
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除部门信息")
    public Result deleteDeptById(@PathVariable Integer id) {
        deptService.removeById(id);
        return Result.success(null);
    }
    //修改部门信息
    @PutMapping
    @Operation(summary = "修改部门信息")
    public Result updateDept(@RequestBody DeptFromDTO deptFromDTO) {
        Dept dept = BeanUtil.copyProperties(deptFromDTO, Dept.class);
        deptService.updateById(dept);
        return Result.success(null);
    }
      //查询部门信息
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询部门信息")
    public Result getDeptById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        DeptVO deptVO = BeanUtil.copyProperties(dept, DeptVO.class);
        return Result.success(deptVO);
    }
    //查询所有部门信息
    @GetMapping("/all")
    @Operation(summary = "查询所有部门信息")
    public Result getAllDept() {
        List<Dept> depts = deptService.list();
        List<DeptVO> deptVOS = BeanUtil.copyToList(depts, DeptVO.class);
        return Result.success(deptVOS);
    }
    }
